package com.nanhu.test.scroll;

import java.util.ArrayList;

import com.nanhu.test.R;
import com.nanhu.test.scroll.ScrollViewMenuActivity.GuidePageAdapter;
import com.nanhu.test.scroll.ScrollViewMenuActivity.GuidePageChangeListener;
import com.nanhu.test.util.SlideMenuUtil;
import com.nanhu.test.widget.compoundview.TengxunMenuBar;
import com.nanhu.test.widget.compoundview.TengxunMenuBar.OnMeneChangeListener;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class ScrollViewMenu3Activity extends Activity{
	
	private ArrayList<View> pageViews;
	
	private ViewPager viewPager;
	
	private LinearLayout menuBarContainer;
	
	private TengxunMenuBar menuBar;
	
	private boolean isFirst;
	
	 /** Called when the activity is first created. */
	private String[] menus = {SlideMenuUtil.ITEM_MOBILE,SlideMenuUtil.ITEM_WEB,
								SlideMenuUtil.ITEM_CLOUD,SlideMenuUtil.ITEM_DATABASE,
								SlideMenuUtil.ITEM_EMBED,SlideMenuUtil.ITEM_SERVER,
								SlideMenuUtil.ITEM_DOTNET,SlideMenuUtil.ITEM_JAVA,
								SlideMenuUtil.ITEM_SAFE,SlideMenuUtil.ITEM_DOMAIN,
								SlideMenuUtil.ITEM_RESEASRCH,SlideMenuUtil.ITEM_MANAGE};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu3);
		menuBarContainer = (LinearLayout)findViewById(R.id.menuBarContainer);
		isFirst = true;
		initPagerView();
		initMenuBar();
	}
	
	private void initMenuBar(){
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = (int)(dm.widthPixels);// 获取分辨率宽度
		menuBar = new TengxunMenuBar(this, menus, screenW);
		menuBarContainer.addView(menuBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		menuBar.setOnMenuChangeListener(new OnMeneChangeListener(){
			@Override
			public void onMenuChange(int toIndex) {
				viewPager.setCurrentItem(toIndex);
			}
		});
	}
	
	private void initPagerView(){
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.item05, null));
        pageViews.add(inflater.inflate(R.layout.item06, null));
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));
        pageViews.add(inflater.inflate(R.layout.item05, null));
        pageViews.add(inflater.inflate(R.layout.item06, null));
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));
        
        viewPager = (ViewPager)findViewById(R.id.guidePagers1);
        viewPager.setTag(true);
        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
        viewPager.setCurrentItem(0);
	}
	
	class GuidePageAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager)container).removeView(pageViews.get(position));
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}
		
		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
			super.restoreState(state, loader);
		}
		
		@Override
		public Parcelable saveState() {
			return super.saveState();
		}
		
		@Override
		public void startUpdate(View container) {
			super.startUpdate(container);
		}
		
		@Override
		public void finishUpdate(View container) {
			super.finishUpdate(container);
		}
	}
	
	class GuidePageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			float toX = arg0 * 80 + 80 * arg1;
			menuBar.moveCursorTo(arg0, arg1);
		}

		@Override
		public void onPageSelected(int arg0) {
			if((Boolean)viewPager.getTag() && arg0 > 1) //横竖屏切换时，需要将光标定位到arg0所指的位置
				menuBar.goTo(arg0, true);
			else
				menuBar.goTo(arg0, false);
			viewPager.setTag(false);
		}
		
	}
}
